<?php
header("Access-Control-Allow-Origin:*");

/**
 * 获取用户基本信息（账户个人等级，评分等）
 * @param unknown_type $pars
 */
function userinfo($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$view_uid=empty($pars->view_uid)?0:intval($pars->view_uid);
		$view_name=empty($pars->view_name)?'':$pars->view_name;

		if(empty($view_uid)&&empty($view_name)){
			throw new Exception('用户不存在');
		}

		$sqlw='';
		if($view_uid){
			$sqlw=" and user_id=$view_uid";
		}
		if($view_name){
			$sqlw=" and user_name='$view_name'";
		}

		$sql="select user_id,user_name,
		case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name,
		headimgurl,city,province,Latitude,Longitude,del,ifnull(fuid,0) as fuid,sign,islogin,gender,birthday,f1.follow_id from cw_member m
		LEFT JOIN cw_follow f1 on m.user_id=f1.outkey and f1.type='user' and f1.uid=".$pars->uid." 
		left join cw_friend f on ((fuid=".$pars->uid." and f.uid=$view_uid and m.user_id=f.uid) or (f.uid=".$pars->uid." and fuid=$view_uid and m.user_id=f.fuid)) and f.status=1 
		where user_type='app' $sqlw";
		$list=$db->getRow($sql);


		if(empty($list)){
			throw new Exception('用户不存在'.$view_uid.$view_name);
		}

		$sql="select count(1) from cw_follow where type='user' and outkey=".$list['user_id'];
		$f=$db->getOne($sql);
		$list['fansnum']=$f;

		$sql="select count(1) from cw_follow where type='user' and uid=".$list['user_id'];
		$f=$db->getOne($sql);
		$list['follownum']=$f;

		$sql="select sum(praisenum) from cw_weibo where uid='".$list['user_id']."'";
		$f=$db->getOne($sql);
		if(empty($f)){
			$f=0;
		}
		$list['praisenum']=$f;

		if($list['del']){
			throw new Exception('用户已经禁止使用');
		}
		$list['age']='';
		$b=$list['birthday'];
		if($b){
			$ny=date('Y');
			$by=date('Y',strtotime($b));
			$list['age']=$ny-$by;
		}

		$list['pet']=array();
		/*
		 $sql="select * from cw_pet where uid=".$list['user_id']." ORDER BY add_time desc limit 2";
		 $pet=$db->getAll($sql);
		 if($pet){
			foreach ($pet as $k=>$v){
			$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
			$list['pet'][]=$v;
			}
			}*/
		/*cjl start*/
		$sql="select * from cw_pet where uid=".$list['user_id']." and ifnull(isDefault,0)=1";
		$pet=$db->getAll($sql);
		if($pet){
			foreach ($pet as $k=>$v){
				$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
				$v['isDefault']=empty($v['isDefault'])?0:intval($v['isDefault']);
				$list['pet'][]=$v;
			}
		}
		$sql="select count(*) as num from cw_pet where uid=".$list['user_id'];
		$list['pet_count']=$db->getOne($sql);

		if(empty($list['pet_count']))
		$list['pet_count']=0;
		/*cjl end*/
		$list['feed']=array();
		$list['weibo']=array();
		$list['video']=array();
		$sql="
select wei_id as id,'weibo' as type,content,pubdate,wei_pic as pic ,file_path,file_pic,follownum as fnum,sharenum as snum,replaynum as rnum,praisenum as pnum,praise_id,follow_id from cw_weibo
LEFT JOIN cw_praise p on cw_weibo.wei_id=p.outkey and p.type='weibo' and p.uid=".$pars->uid." 
LEFT JOIN cw_follow f on cw_weibo.wei_id=f.outkey and f.type='weibo' and f.uid=".$pars->uid."  
where cw_weibo.uid=".$list['user_id']." ORDER BY pubdate desc limit 0,10";
		$feed=$db->getAll($sql);
		if($feed){
			foreach ($feed as $k=>$v){
				$v['pubdate']=local_date('Y-m-d H:i',$v['pubdate']);
				$list['feed'][]=$v;
			}
		}

		$sql="
select wei_id as id,'weibo' as type,content,pubdate,wei_pic as pic ,file_path,file_pic,follownum as fnum,sharenum as snum,replaynum as rnum,praisenum as pnum,praise_id,follow_id from cw_weibo
LEFT JOIN cw_praise p on cw_weibo.wei_id=p.outkey and p.type='weibo' and p.uid=".$pars->uid." 
LEFT JOIN cw_follow f on cw_weibo.wei_id=f.outkey and f.type='weibo' and f.uid=".$pars->uid." 
where cw_weibo.uid=".$list['user_id']." and ifnull(file_path,'')='' ORDER BY pubdate desc limit 0,10";
		$weibo=$db->getAll($sql);
		if($weibo){
			foreach ($weibo as $k=>$v){
				$v['pubdate']=local_date('Y-m-d H:i',$v['pubdate']);
				$list['weibo'][]=$v;
			}
		}

		$sql="
select wei_id as id,'weibo' as type,content,pubdate,wei_pic as pic ,file_path,file_pic,follownum as fnum,sharenum as snum,replaynum as rnum,praisenum as pnum,praise_id,follow_id from cw_weibo
LEFT JOIN cw_praise p on cw_weibo.wei_id=p.outkey and p.type='weibo' and p.uid=".$pars->uid." 
LEFT JOIN cw_follow f on cw_weibo.wei_id=f.outkey and f.type='weibo' and f.uid=".$pars->uid." 
where cw_weibo.uid=".$list['user_id']." and ifnull(file_path,'')<>'' ORDER BY pubdate desc limit 0,10";
		$weibo=$db->getAll($sql);
		if($weibo){
			foreach ($weibo as $k=>$v){
				$v['pubdate']=local_date('Y-m-d H:i',$v['pubdate']);
				$list['video'][]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 用户动态
 * @param unknown_type $pars
 */
function userfeed($pars){
	$db =&db();
	$status=0;
	try{
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$view_uid=empty($pars->view_uid)?0:intval($pars->view_uid);
		if(empty($view_uid)){
			throw new Exception('用户不存在');
		}

		$sql="select del from cw_member where user_type='app' and user_id=$view_uid";
		$user=$db->getRow($sql);
		if(empty($user)){
			throw new Exception('用户不存在');
		}

		if($user['del']){
			throw new Exception('用户已经禁止使用');
		}

		$sql="
select wei_id as id,'weibo' as type,content,pubdate,wei_pic as pic ,file_path,file_pic,follownum as fnum,sharenum as snum,replaynum as rnum,praisenum as pnum,praise_id,
f.follow_id,f1.follow_id as ufollow_id from cw_weibo
LEFT JOIN cw_praise p on cw_weibo.wei_id=p.outkey and p.type='weibo' and p.uid=".$pars->uid."  
LEFT JOIN cw_follow f1 on cw_weibo.uid=f1.outkey and f1.type='user' and f1.uid=".$pars->uid." 
LEFT JOIN cw_follow f on cw_weibo.wei_id=f.outkey and f.type='weibo' and f.uid=".$pars->uid." 
where cw_weibo.uid=".$pars->view_uid." ORDER BY pubdate desc limit $row,$size";

		$feed=$db->getAll($sql);
		if($feed){
			foreach ($feed as $k=>$v){
				$v['pubdate']=local_date('Y-m-d H:i',$v['pubdate']);
				$list[]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 用户微博
 * @param unknown_type $pars
 */
function userweibo($pars){
	$db =&db();
	$status=0;
	try{
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$view_uid=empty($pars->view_uid)?0:intval($pars->view_uid);
		if(empty($view_uid)){
			throw new Exception('用户不存在');
		}

		$sql="select del from cw_member where user_type='app' and user_id=$view_uid";
		$user=$db->getRow($sql);
		if(empty($user)){
			throw new Exception('用户不存在');
		}

		if($user['del']){
			throw new Exception('用户已经禁止使用');
		}

		$sql="select * from (
select wei_id as id,'weibo' as type,content,pubdate,wei_pic as pic ,'' as file_path,follownum as fnum,sharenum as snum,replaynum as rnum,praisenum as pnum,praise_id,follow_id from cw_weibo
LEFT JOIN cw_praise p on cw_weibo.wei_id=p.outkey and p.type='weibo' and p.uid=".$pars->uid."   
LEFT JOIN cw_follow f on cw_weibo.wei_id=f.outkey and f.type='weibo' and f.uid=".$pars->uid." 
where cw_weibo.uid=".$pars->view_uid.")
 x ORDER BY pubdate desc limit $row,$size";
		$feed=$db->getAll($sql);
		if($feed){
			foreach ($feed as $k=>$v){
				$v['pubdate']=local_date('Y-m-d H:i',$v['pubdate']);
				$list[]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}


/**
 * 用户视频
 * @param unknown_type $pars
 */
function uservideo($pars){
	$db =&db();
	$status=0;
	try{
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$view_uid=empty($pars->view_uid)?0:intval($pars->view_uid);
		if(empty($view_uid)){
			throw new Exception('用户不存在');
		}

		$sql="select del from cw_member where user_type='app' and user_id=$view_uid";
		$user=$db->getRow($sql);
		if(empty($user)){
			throw new Exception('用户不存在');
		}

		if($user['del']){
			throw new Exception('用户已经禁止使用');
		}

		$sql="select * from (
select video_id as id,'video' as type,content,pubdate,pic,file_path,follow_num as fnum,share_num as snum,comment_num as rnum,praise_num as pnum,praise_id from cw_video
LEFT JOIN cw_praise p on cw_video.video_id=p.outkey and p.type='video' and p.uid=".$pars->uid."  
where cw_video.uid=".$pars->view_uid." and checked=1)
 x ORDER BY pubdate desc limit $row,$size";
		$feed=$db->getAll($sql);
		if($feed){
			foreach ($feed as $k=>$v){
				$v['pubdate']=local_date('Y-m-d H:i',$v['pubdate']);
				$list[]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 距离定义
 * @param unknown_type $pars
 */
function metre($pars){
	$db =&db();
	$status=0;
	try{
		$sql="select d_key,d_value from cw_dictionary where d_code='metre' order by d_order";
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 非好友用户列表(加好友)
 * @param unknown_type $pars
 * @throws Exception
 */
function userlist($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$pars->lat=empty($pars->lat)?0:floatval($pars->lat);
		$pars->lng=empty($pars->lng)?0:floatval($pars->lng);
		$pars->sex=empty($pars->sex)?0:floatval($pars->sex);
		$pars->psex=empty($pars->psex)?'':$pars->psex;
		$sort=empty($pars->sort)?'logins':$pars->sort;
		$order=empty($pars->order)?'desc':$pars->order;
		$sqlw=" 1=1";
		$cate_id=empty($pars->cate_id)?0:$pars->cate_id;

		$metre=empty($pars->metre)?0:$pars->metre;

		$name=empty($pars->name)?'':$pars->name;

		if($cate_id){
			$sqlw.=" and exists(
select 1 from cw_pet p 
LEFT JOIN cw_pcategory c on p.cate_id=c.cate_id 
LEFT JOIN cw_pcategory c1 on c.parent_id=c1.cate_id
where p.uid=user_id and (c.cate_id=$cate_id or c1.cate_id=$cate_id))";
		}
		if($metre){
			switch($metre){
				case 1:
					$sqlw.=' and ( metre> 0 and metre<=1000 )';
					break;
				case 2:
					$sqlw.=' and ( metre> 1000 and metre<=2000 )';
					break;
				case 3:
					$sqlw.=' and ( metre> 2000 and metre<=5000 )';
					break;
				case 4:
					$sqlw.=' and ( metre> 5000 )';
					break;
			}
				
			//$sqlw.=" and round(6378.138*2*asin(sqrt(pow(sin((".$pars->lat."*pi()/180-Latitude*pi()/180)/2),2)+cos(".$pars->lat."*pi()/180)*cos(Latitude*pi()/180)* pow(sin((".$pars->lng."*pi()/180-Longitude*pi()/180)/2),2)))*1000)<=$metre";
		}

		if($name){
			//$sqlw.=" and (user_name like '%".$name."%' or real_name like '%".$name."%')";
			$sqlw.=" and (user_name like '%".$name."%' or real_name like '%".$name."%' or exists(
					select uid from cw_pet where (`name` like '%".$name."%' or cate like '%".$name."%') and uid=user_id))";	
		}

		if($pars->sex){
			$sqlw.=" and gender=$pars->sex ";
		}

		if($pars->psex){
			$sqlw.=" and exists(
					select 1 from cw_pet where sex='".$pars->psex."')";	
		}

		$sql="select user_id,user_name,
		case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name
		,headimgurl,city,province,gender,sign,islogin,
round(6378.138*2*asin(sqrt(pow(sin((".$pars->lat."*pi()/180-Latitude*pi()/180)/2),2)+cos(".$pars->lat."*pi()/180)*cos(Latitude*pi()/180)* pow(sin((".$pars->lng."*pi()/180-Longitude*pi()/180)/2),2)))*1000) as metre
,CASE WHEN ifnull(birthday,'')<>'' THEN DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP()),'%Y')-DATE_FORMAT(birthday,'%Y') ELSE '' end as age 
from cw_member m
where $sqlw and del=0 and not EXISTS(select 1 from cw_friend f where ((m.user_id=f.uid and fuid=".$pars->uid.") or (m.user_id=f.fuid and uid=".$pars->uid."))) and user_id!=".$pars->uid." 
order by $sort $order,user_id desc limit $row,$size";

		$ulist=$db->getAll($sql);

		if($ulist){

			foreach ($ulist as $k=>$v){
				$sql="select * from cw_pet where uid=".$v['user_id']." ORDER BY add_time desc limit 1";
				$pet=$db->getRow($sql);
				if($pet){
					$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
					$v['pet']=$pet;
				}
				$list[]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 添加好友
 * @param unknown_type $pars
 * @throws Exception
 */
function addfriend($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$fuid=empty($pars->fuid)?0:intval($pars->fuid);
		$message=empty($pars->message)?'':$pars->message;
		if(empty($fuid)){
			throw new Exception('用户不存在');
		}

		if($pars->uid==$fuid){
			throw new Exception('不能加自己为好友');
		}

		$sql="select user_id,fuid,f.status,del from cw_member m
		left join cw_friend f on ((fuid=".$pars->uid." and uid=$fuid and m.user_id=f.uid) or (uid=".$pars->uid." and fuid=$fuid and m.user_id=f.fuid))
		where user_type='app' and user_id=$fuid";
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('用户不存在');
		}

		if($list['del']){
			throw new Exception('用户已经禁止使用');
		}

		if(empty($list['fuid'])){
			$sql="insert into cw_friend set uid=".$pars->uid.",fuid=$fuid,message='".$message."',add_time='".gmtime()."'";
			$db->query($sql);

			$list="申请好友成功,耐心等待对方同意";
		}else{
			if(empty($list['status'])){
				if($pars->uid==$list['fuid']){
					$sql="update cw_friend set status=1,agree_time=".gmtime()." where uid=$fuid and fuid=".$pars->uid;
					$db->query($sql);

					$list="添加好友成功";
				}else{
					throw new Exception('你已经申请添加Ta为好友,耐心等待对方同意');
				}
			}else{
				throw new Exception('你和Ta已经是好友了');
			}
		}


		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 好友列表
 * @param unknown_type $pars
 * @throws Exception
 */
function friendlist($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$pars->lat=empty($pars->lat)?0:floatval($pars->lat);
		$pars->lng=empty($pars->lng)?0:floatval($pars->lng);


		$sql="select user_id,user_name,
		case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name
		,headimgurl,city,province,gender,sign,islogin,
round(6378.138*2*asin(sqrt(pow(sin((".$pars->lat."*pi()/180-Latitude*pi()/180)/2),2)+cos(".$pars->lat."*pi()/180)*cos(Latitude*pi()/180)* pow(sin((".$pars->lng."*pi()/180-Longitude*pi()/180)/2),2)))*1000) as metre
,CASE WHEN ifnull(birthday,'')<>'' THEN DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP()),'%Y')-DATE_FORMAT(birthday,'%Y') ELSE '' end as age  
from cw_member m
 inner join cw_friend f on ((m.user_id=f.uid and fuid=".$pars->uid.") or (m.user_id=f.fuid and uid=".$pars->uid.")) and f.status=1
where del=0 and user_id!=".$pars->uid." 
order by agree_time desc limit $row,$size";
		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 申请加我为好友的用户（未同意）
 * @param unknown_type $pars
 * @throws Exception
 */
function wantfriendlist($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$pars->lat=empty($pars->lat)?0:floatval($pars->lat);
		$pars->lng=empty($pars->lng)?0:floatval($pars->lng);

		$sql="select user_id,user_name,
		case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name
		,headimgurl,city,province,sex,sign,islogin,
round(6378.138*2*asin(sqrt(pow(sin((".$pars->lat."*pi()/180-Latitude*pi()/180)/2),2)+cos(".$pars->lat."*pi()/180)*cos(Latitude*pi()/180)* pow(sin((".$pars->lng."*pi()/180-Longitude*pi()/180)/2),2)))*1000) as metre
 ,f.message from cw_member m
 inner join cw_friend f on m.user_id=f.uid and fuid=".$pars->uid." and f.status=0
where del=0 and user_id!=".$pars->uid." 
order by agree_time desc limit $row,$size";
		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 申请加我为好友的用户（未同意）
 * @param unknown_type $pars
 * @throws Exception
 */
function wantfrienduser($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$pars->lat=empty($pars->lat)?0:floatval($pars->lat);
		$pars->lng=empty($pars->lng)?0:floatval($pars->lng);

		$fuid=empty($pars->fuid)?0:intval($pars->fuid);

		$sql="select user_id,user_name,
		case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name
		,headimgurl,city,province,sex,sign,islogin,
round(6378.138*2*asin(sqrt(pow(sin((".$pars->lat."*pi()/180-Latitude*pi()/180)/2),2)+cos(".$pars->lat."*pi()/180)*cos(Latitude*pi()/180)* pow(sin((".$pars->lng."*pi()/180-Longitude*pi()/180)/2),2)))*1000) as metre
 ,f.message ,f.`status` from cw_member m
 inner join cw_friend f on m.user_id=f.uid and fuid=".$pars->uid." and f.status=0 and f.uid=$fuid
where del=0 and user_id!=".$pars->uid." 
order by agree_time desc limit $row,$size";
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('他还没有申请加你为好友');
		}


		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 处理好友申请
 * @param unknown_type $pars
 * @throws Exception
 */
function dealaddfriend($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$fuid=empty($pars->fuid)?0:intval($pars->fuid);
		$deal=!isset($pars->deal)?1:intval($pars->deal);
		if(empty($fuid)){
			throw new Exception('用户不存在');
		}

		$sql="select 1 from cw_member m
 inner join cw_friend f on m.user_id=f.uid and fuid=".$pars->uid." and f.status=0
where del=0 and user_id=".$fuid;
		$list=$db->getRow($sql);

		if(empty($list)){
			throw new Exception('好友申请不存在');
		}


		if($deal){
			$sql="update cw_friend set status=1,agree_time=".gmtime()." where uid=$fuid and fuid=".$pars->uid;
			$db->query($sql);

			$list="添加好友成功";
		}else{
			$sql="delete from cw_friend where uid=$fuid and fuid=".$pars->uid;
			$db->query($sql);

			$list="你已经拒绝Ta的好友请求";
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 删除好友
 * @param unknown_type $pars
 * @throws Exception
 */
function delfriend($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$fuid=empty($pars->fuid)?0:intval($pars->fuid);
		if(empty($fuid)){
			throw new Exception('用户不存在');
		}

		$sql="select 1 from cw_member m
 inner join cw_friend f on ((fuid=".$pars->uid." and uid=$fuid and m.user_id=f.uid) or (uid=".$pars->uid." and fuid=$fuid and m.user_id=f.fuid)) and f.status=1
where del=0 and user_id=".$fuid;
		$list=$db->getRow($sql);

		if(empty($list)){
			throw new Exception('对方还不是你的好友');
		}

		$sql="delete from cw_friend where ((fuid=".$pars->uid." and uid=$fuid) or (uid=".$pars->uid." and fuid=$fuid))";
		$db->query($sql);

		if(!$db->affected_rows()){
			throw new Exception('删除好友失败');
		}

		$list="删除好友成功";

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 用户中心
 * @param unknown_type $pars
 * @throws Exception
 */
function usercenter($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$sql="select user_id,user_name,real_name,headimgurl,city,province,Latitude,Longitude,del,sign,islogin,gender,birthday from cw_member
		where user_type='app' and user_id=$pars->uid";
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('用户不存在');
		}

		if($list['del']){
			throw new Exception('用户已经禁止使用');
		}

		$sql="select count(1) from cw_follow where type='user' and outkey=".$pars->uid;
		$f=$db->getOne($sql);
		$list['fansnum']=$f;

		$sql="select count(1) from cw_follow where type='user' and uid=".$pars->uid;
		$f=$db->getOne($sql);
		$list['follownum']=$f;

		$sql="select sum(praisenum) from cw_weibo where uid='".$pars->uid."'";
		$f=$db->getOne($sql);
		if(empty($f)){
			$f=0;
		}
		$list['praisenum']=$f;

		$list['age']='';
		$b=$list['birthday'];
		if($b){
			$ny=date('Y');
			$by=date('Y',strtotime($b));
			$list['age']=$ny-$by;
		}

		$list['pet']=array();
		//		$sql="select * from cw_pet where uid=".$pars->uid." ORDER BY add_time desc limit 2";
		//		$pet=$db->getAll($sql);
		//		if($pet){
		//			foreach ($pet as $k=>$v){
		//				$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
		//				$list['pet'][]=$v;
		//			}
		//		}
		/*cjl start*/
		$sql="select * from cw_pet where uid=".$list['user_id']." and ifnull(isDefault,0)=1";
		$pet=$db->getAll($sql);
		if($pet){
			foreach ($pet as $k=>$v){
				$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
				$v['isDefault']=empty($v['isDefault'])?0:intval($v['isDefault']);
				$list['pet'][]=$v;
			}
		}
		$sql="select count(*) as num from cw_pet where uid=".$list['user_id'];
		$list['pet_count']=$db->getOne($sql);

		if(empty($list['pet_count']))
		$list['pet_count']=0;
		/*cjl end*/
		$sql="select count(1) from cw_friend where uid=".$pars->uid." and `status`=1";
		$list['like']=$db->getOne($sql);

		$sql="select count(1) from cw_friend where fuid=".$pars->uid." and `status`=1";
		$list['fans']=$db->getOne($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 粉丝
 * @param unknown_type $pars
 * @throws Exception
 */
function fans($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$sql="select user_id,user_name,real_name,headimgurl,fodate,follow_id from cw_follow f
		LEFT JOIN cw_member m on m.user_id=f.uid
		where type='user' and outkey=".$pars->uid." order by fodate desc limit $row,$size";
		$alist=$db->getAll($sql);
		if($alist){
			foreach ($alist as $k=>$v){
				$v['fodate']=local_date('Y-m-d H:i',$v['fodate']);
				$list[]=$v;
			}
		}
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}
/**
 * 贊
 * @param unknown_type $pars
 * @throws Exception
 */
function zan($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;
		$sql="select user_id,user_name,real_name,headimgurl,padddate as fodate,praise_id from cw_praise p
		LEFT JOIN cw_member m on m.user_id=p.outkey
		where type='weibo' and p.uid=".$pars->uid." order by padddate desc limit $row,$size";
		$alist=$db->getAll($sql);
		if($alist){
			foreach ($alist as $k=>$v){
				$v['fodate']=local_date('Y-m-d H:i',$v['fodate']);
				$list[]=$v;
			}
		}
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}




?>